#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
导数与统计流程：
1、根据查询条件从mongo得到数据集
2、遍历数据集，筛选符合条件数据进行导出或者统计
'''

import json
import sys
sys.path.append('/mnt/moonbrook/Moonbrook')
import subprocess
import os
import re
import psutil
import time
import requests
from copy import deepcopy
from datetime import datetime, timedelta
from pybloom_live import ScalableBloomFilter
from secret import batch_encrypt_decrypt_dict
from aes import contact_aes, name_aes
from apscheduler.schedulers.blocking import BlockingScheduler
from datastore.mongodb import MongoDBClientWrapper

from base import ExportAndTjBase
from utils import Utils
from models import MongoQueryConfig, RobotConfig
from robot import DingtalkRobot

reload(sys)
sys.setdefaultencoding('utf-8')


class ExportAndTj(ExportAndTjBase):
    def __init__(self, *args, **kwargs):
        super(ExportAndTj, self).__init__(*args, **kwargs)
        # todo other col
        # self.contact_col = self.db.p_col('contact')
        self.ent_col = self.db.p_col('enterprise')
        self.init_file_2(self.file_name)

    def init_file_2(self, abs_file_name, mod='w'):
        dir_path = os.path.dirname(abs_file_name)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
        self.file_dir_path = dir_path

        self.write_mobile_dc_f = open('{}_mobile_dc'.format(abs_file_name), mod)  # 导出到文件
        self.write_mobile_api_dc_f = open('{}_mobile_api_dc'.format(abs_file_name), mod)  # 导出到文件
        self.write_phone_dc_f = open('{}_phone_dc'.format(abs_file_name), mod)  # 导出到文件
        self.write_phone_api_dc_f = open('{}_phone_api_dc'.format(abs_file_name), mod)  # 导出到文件
        self.write_email_dc_f = open('{}_email_dc'.format(abs_file_name), mod)  # 导出到文件
        self.write_email_api_dc_f = open('{}_email_api_dc'.format(abs_file_name), mod)  # 导出到文件
        # 6月12之后的
        self.write_mobile_afte_dc_f = open('{}_mobile_afte_dc'.format(abs_file_name), mod)  # 导出到文件
        self.write_mobile_afte_api_dc_f = open('{}_mobile_afte_api_dc'.format(abs_file_name), mod)  # 导出到文件
        self.write_phone_afte_dc_f = open('{}_phone_afte_dc'.format(abs_file_name), mod)  # 导出到文件
        self.write_phone_afte_api_dc_f = open('{}_phone_afte_api_dc'.format(abs_file_name), mod)  # 导出到文件
        self.write_email_afte_dc_f = open('{}_email_afte_dc'.format(abs_file_name), mod)  # 导出到文件
        self.write_email_afte_api_dc_f = open('{}_email_afte_api_dc'.format(abs_file_name), mod)  # 导出到文件

    def close_file_2(self):
        self.write_mobile_dc_f.close()
        self.write_mobile_api_dc_f.close()
        self.write_phone_dc_f.close()
        self.write_phone_api_dc_f.close()
        self.write_email_dc_f.close()
        self.write_email_api_dc_f.close()

        # 6月12之后的
        self.write_mobile_afte_dc_f.close()
        self.write_mobile_afte_api_dc_f.close()
        self.write_phone_afte_dc_f.close()
        self.write_phone_afte_api_dc_f.close()
        self.write_email_afte_dc_f.close()
        self.write_email_afte_api_dc_f.close()

    def handle_one_mongo_data(self, record_dic, save_result_dic=None):
        record_dic = Utils.decrypt_one_record(record_dic, need_decrypt=True, need_strf_time=True)
        contactChannelList = record_dic.get('contactChannelList')
        contact = record_dic.get('contact')
        contactType = record_dic.get('contactType')
        create_time = record_dic.get('create_time')
        import_update_time = record_dic.get('import_update_time')
        nameId = record_dic.get('nameId')
        name = record_dic.get('name')
        _id = record_dic.get('_id')
        if len(contactChannelList) <= 1:
            return save_result_dic
        page_link = contactChannelList[0].get('contactPageLink')
        if not page_link:
            return save_result_dic

        if not name:
            dic = self.ent_col.find_one({'_id': nameId}, projection=['name'])
            if isinstance(dic, dict):
                ent_dic = Utils.decrypt_one_record(dic, need_strf_time=True, need_decrypt=True)
                name = ent_dic.get('name')

        is_api_flag =  True if page_link.count('ViewTel') else False
        dc_info = '{}\t{}\t{}\t{}\t{}\t{}\t{}\n'.format(nameId, contact, page_link, create_time, import_update_time, name, _id)
        # 来源仅为atob时间 -- 类型
        if '2021-03-20' <= create_time < '2021-06-12':  # 之前
            # 之前的数量：是接口的数量，非接口的数量
            if contactType == 0:
                if is_api_flag:
                    save_result_dic['is_api_mobile_count'] += 1
                    if save_result_dic['is_api_mobile_count']<100:
                        self.write_mobile_api_dc_f.write(dc_info)
                else:
                    save_result_dic['not_api_mobile_count'] += 1
                    if save_result_dic['not_api_mobile_count']<100:
                        self.write_mobile_dc_f.write(dc_info)
            elif contactType == 1:
                if is_api_flag:
                    save_result_dic['is_api_phone_count'] += 1
                    if save_result_dic['is_api_phone_count']<100:
                        self.write_phone_api_dc_f.write(dc_info)
                else:
                    save_result_dic['not_api_phone_count'] += 1
                    if save_result_dic['not_api_phone_count']<100:
                        self.write_phone_dc_f.write(dc_info)
            elif contactType == 2:
                if is_api_flag:
                    save_result_dic['is_api_email_count'] += 1
                    if save_result_dic['is_api_email_count']<100:
                        self.write_email_api_dc_f.write(dc_info)
                else:
                    save_result_dic['not_api_email_count'] += 1
                    if save_result_dic['not_api_email_count']<100:
                        self.write_email_dc_f.write(dc_info)
        elif create_time >= '2021-06-12': # 之后
            # 之后的数量：是接口的数量，非接口的数量
            if contactType == 0:
                if is_api_flag:
                    save_result_dic['is_api_mobile_count_after'] += 1
                    if save_result_dic['is_api_mobile_count_after'] < 100:
                        self.write_mobile_afte_api_dc_f.write(dc_info)
                else:
                    save_result_dic['not_api_mobile_count'] += 1
                    if save_result_dic['not_api_mobile_count'] < 100:
                        self.write_mobile_afte_dc_f.write(dc_info)
            elif contactType == 1:
                if is_api_flag:
                    save_result_dic['is_api_phone_count_after'] += 1
                    if save_result_dic['is_api_phone_count_after'] < 100:
                        self.write_phone_afte_api_dc_f.write(dc_info)
                else:
                    save_result_dic['not_api_phone_count_after'] += 1
                    if save_result_dic['not_api_phone_count_after'] < 100:
                        self.write_phone_afte_dc_f.write(dc_info)
            elif contactType == 2:
                if is_api_flag:
                    save_result_dic['is_api_email_count_after'] += 1
                    if save_result_dic['is_api_email_count_after'] < 100:
                        self.write_email_afte_api_dc_f.write(dc_info)
                else:
                    save_result_dic['not_api_email_count_after'] += 1
                    if save_result_dic['not_api_email_count_after'] < 100:
                        self.write_email_afte_dc_f.write(dc_info)


        return save_result_dic

    def run(self):

        save_result_dic = {
            'is_api_mobile_count':0,
            'not_api_mobile_count':0,
            'is_api_mobile_count_before': 0,
            'not_api_mobile_count_before': 0,
            'is_api_mobile_count_after': 0,
            'not_api_mobile_count_after': 0,

            'is_api_phone_count': 0,
            'not_api_phone_count': 0,
            'is_api_phone_count_before': 0,
            'not_api_phone_count_before': 0,
            'is_api_phone_count_after': 0,
            'not_api_phone_count_after': 0,

            'is_api_email_count': 0,
            'not_api_email_count': 0,
            'is_api_email_count_before': 0,
            'not_api_email_count_before': 0,
            'is_api_email_count_after': 0,
            'not_api_email_count_after': 0,
        }
        config = {
            MongoQueryConfig.col_name: 'contact',
            MongoQueryConfig.filter: {
                'contactType': {"$in": [0, 1, 2]},
                "contactChannelList.contactSource": re.compile(r'atobo$'),
            },
            MongoQueryConfig.projection: ["contact","nameId","create_time","_id","contactChannelList","contactType","import_update_time","name"]
        }

        result_dic = self.get_data_from_mongo(config, save_result_dic)

        self.close_file_2()

    def make_mk_text(self, result_dic):
        '''
        通过统计结果dict 拼接出mk_text
        @param result_dic: 统计结果dict
        @return: 发给钉钉的内容  str
        '''
        mk_text = ''
        for source, v in result_dic.items():
            all_dp_length = v.get('all', 0)  # 店铺url总数
            ys_dp_length = v.get('ys', 0)  # 可映射店铺url总数
            source_ys_set = v.get('dup', '')
            ys_dp_dup_length = len(source_ys_set)  # 可映射店铺去重总数（数据源）

            ys_add_length = v.get('ys_add', 0)  # 昨日店铺新增url总数
            ys_add_dup_length = len(v.get('ys_add_ys_dup', ''))  # 昨天新增可映射店铺去重总数（数据源）
            ys_update_length = v.get('ys_update', 0)  # 昨日店铺更新url总数
            ys_update_dup_length = len(v.get('ys_update_ys_dup', ''))  # 昨天更新可映射店铺去重总数（数据源）
            text = '''  \n**{}**  
            **店铺url总数：{}**  
            昨日更新数：{}  
            昨日新增数：{}  
            **可映射主体数：{}**  
            昨日更新数：{}  
            昨日新增数：{}
            '''.format(source, all_dp_length,
                       ys_update_length,
                       ys_add_length, ys_dp_dup_length,
                       ys_update_dup_length,
                       ys_add_dup_length)
            mk_text += text

        all_table_bloom_length = 1
        all_table_bloom_ys_update_length = 2
        all_table_bloom_ys_add_length = 3
        mk_text = '''**店铺映射主体总数(全表去重)：{}**  \n**昨日更新数：{}**  \n**昨日新增数：{}**  \n'''.format(all_table_bloom_length,
                                                                                         all_table_bloom_ys_update_length,
                                                                                         all_table_bloom_ys_add_length) + mk_text
        return mk_text


def run(abs_file_name):
    export_and_tj = ExportAndTj(abs_file_name)
    export_and_tj.main()


def scheduler_run(abs_file_name):
    scheduler = BlockingScheduler()
    scheduler.add_job(func=run, trigger='cron',
                      args=(abs_file_name,),
                      # day_of_week="sun",
                      hour='20', minute='52', id='dd_run')
    scheduler.start()


if __name__ == '__main__':
    file_name = '来源v1统计'
    # py文件名+file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name,current_py_path=__file__)

    run(abs_file_name)
    # scheduler_run(abs_file_name)


